package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.apache.zookeeper.Op;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author XJQ
 * @date 2019-12-01 8:50
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemDao checkItemDao;

    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult queryPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        String queryString = queryPageBean.getQueryString();
        if (queryString !=null){
            queryString = "%"+queryString+"%";
            queryPageBean.setQueryString(queryString);
        }
        List<CheckItem> checkItems = checkItemDao.queryPage(queryPageBean);
        PageInfo<CheckItem> pageInfo =new PageInfo<CheckItem>(checkItems);
        long total = pageInfo.getTotal();
        return new PageResult(total,checkItems);


    }

    @Override
    public void deleteById(Integer id) {
        long count = checkItemDao.findCountByCheckItemId(id);
        if (count >0){
            //已被关联，不许删除
            new RuntimeException();
        }else {
            checkItemDao.deleteById(id);
        }
    }
}
